package cn.lingyangwl.agile.system.service.impl;

import cn.lingyangwl.agile.model.constants.OAuth2Cons;
import cn.lingyangwl.agile.model.module.auth.LoginUser;
import cn.lingyangwl.agile.system.model.entity.SysUserOnline;
import cn.lingyangwl.agile.system.service.SysUserOnlineService;
import cn.lingyangwl.framework.cache.core.service.CacheService;
import cn.lingyangwl.framework.tool.core.CollectionUtils;
import cn.lingyangwl.framework.tool.core.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author shenguangyang
 */
@Service
public class SysUserOnlineServiceImpl implements SysUserOnlineService {
    @Resource
    private CacheService cacheService;

    /**
     * 通过登录地址查询信息
     *
     * @param ipaddr 登录地址
     * @param user 用户信息
     * @return 在线用户信息
     */
    @Override
    public SysUserOnline selectOnlineByIpaddr(String ipaddr, LoginUser user) {
        if (StringUtils.equals(ipaddr, user.getLoginIp())) {
            return loginUserToUserOnline(user);
        }
        return null;
    }

    /**
     * 通过用户名称查询信息
     *
     * @param userName 用户名称
     * @param user 用户信息
     * @return 在线用户信息
     */
    @Override
    public SysUserOnline selectOnlineByuserName(String userName, LoginUser user) {
        if (StringUtils.equals(userName, user.getUserName())) {
            return loginUserToUserOnline(user);
        }
        return null;
    }

    /**
     * 通过登录地址/用户名称查询信息
     *
     * @param ipaddr 登录地址
     * @param userName 用户名称
     * @param user 用户信息
     * @return 在线用户信息
     */
    @Override
    public SysUserOnline selectOnlineByInfo(String ipaddr, String userName, LoginUser user) {
        if (StringUtils.equals(ipaddr, user.getLoginIp()) && StringUtils.equals(userName, user.getUserName())) {
            return loginUserToUserOnline(user);
        }
        return null;
    }

    /**
     * 设置在线用户信息
     *
     * @param user 用户信息
     * @return 在线用户
     */
    @Override
    public SysUserOnline loginUserToUserOnline(LoginUser user) {
        if (StringUtils.isNull(user)) {
            return null;
        }
//        List<SysDept> deptList = sysDeptService.listDeptByIds(user.getDeptIds());
//        List<String> deptNames = deptList.stream().map(SysDept::getDeptName).collect(Collectors.toList());
        SysUserOnline sysUserOnline = new SysUserOnline();
        sysUserOnline.setId(user.getUserId());
        sysUserOnline.setUserName(user.getUserName());
        sysUserOnline.setIpAddr(user.getLoginIp());
        sysUserOnline.setLoginTime(user.getLoginTime());
        sysUserOnline.setBrowser(user.getBrowser());
        sysUserOnline.setLoginLocation(user.getLoginLocation());
        sysUserOnline.setOs(user.getOs());
//        sysUserOnline.setDeptNames(deptNames);
        return sysUserOnline;
    }

    @Override
    public void forceLogoutBatch(String userIdStr) {
        List<Long> userIdList = StringUtils.toList(userIdStr, ",", Long.class);
        if (CollectionUtils.isNotEmpty(userIdList)) {
            for (Long userId : userIdList) {
                String cacheKey = String.format(OAuth2Cons.CACHE_ACCESS_TOKEN, userId, "*");
                Set<String> keys = cacheService.opsForKey().keys(cacheKey);
                cacheService.opsForKey().delete(keys);
            }
        }
    }
}
